package com.basic.business.common.execption;


import com.basic.business.common.constant.ResultMsgEnum;
import lombok.Data;

@Data
public class BusinessException extends RuntimeException {
    // 错误码
    protected int code;

    //错误消息
    protected String msg;

    // 参数
    protected String[] params;

    public BusinessException() {
        super();
    }

    /**
     * 用错误信息构造
     *
     * @param msg msg
     */
    public BusinessException(String msg) {
        super(msg);
        this.msg = msg;
        this.code = ResultMsgEnum.FAIL.getCode();
    }

    public BusinessException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    /**
     * 创建异常
     *
     * @param msg msg
     * @param ex  异常
     */
    public BusinessException(String msg, Throwable ex) {
        super(ex);
        this.msg = msg;
    }

    /**
     * 创建异常
     *
     * @param msg    msg
     * @param params 参数
     */
    public BusinessException(String msg, String[] params) {
        this.msg = msg;
        this.params = params;
    }

    /**
     * 创建异常
     *
     * @param msg    msg
     * @param ex     异常
     * @param params 参数
     */
    public BusinessException(String msg, Throwable ex, String[] params) {
        super(ex);
        this.msg = msg;
        this.params = params;
    }


    /**
     * 取参数
     *
     * @return 去参数
     */
    public String[] getParams() {
        return params;
    }

    public void setParams(String[] params) {
        this.params = params;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(msg);
        if (params != null && params.length > 0) {
            for (Object s : params) {
                sb.append(",").append(s);
            }
        }
        return sb.toString();
    }
}
